package com.health.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.health.system.annotation.Log;
import com.health.system.common.result.Result;
import com.health.system.dto.AdminUserListPageDTO;
import com.health.system.dto.UserOnlineDTO;
import com.health.system.service.UserAuthService;
import com.health.system.service.UserInfoService;
import com.health.system.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.health.system.constant.LogTypeConstant.*;


@Api(tags = "用户模块")
@RestController
public class UserController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserAuthService userAuthService;

    @Log(SELECT)
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "current", value = "当前页", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "size", value = "每页显示数量", dataTypeClass = Long.class)
            }
    )
    @ApiOperation(value = "用户列表")
    @GetMapping("/admin/user/listPage")
    public Result<IPage<AdminUserListPageDTO>> listPage(@RequestParam(value = "current", defaultValue = "1") Long current,
                                                        @RequestParam(value = "size", defaultValue = "10") Long size,
                                                        UserListPageSearchVo condition) {
        IPage<AdminUserListPageDTO> result = userAuthService.listPage(current, size, condition);
        return Result.ok(result);
    }

    @Log(UPDATE)
    @ApiOperation(value = "修改信息")
    @PostMapping("/admin/user/info")
    public Result<String> updateInfo(@Valid @RequestBody ModifyUserInfoVo vo) {
        userInfoService.updateInfoById(vo);
        return Result.ok();
    }

    @Log(UPDATE)
    @ApiOperation(value = "用户编辑")
    @PostMapping("/admin/user/update")
    public Result<String> updateById(@Valid @RequestBody ModifyUserVo user) {
        userAuthService.updateById(user);
        return Result.ok();
    }

    @Log(UPDATE)
    @ApiOperation(value = "修改密码")
    @PostMapping("/admin/user/password")
    public Result<String> updatePassword(@Valid @RequestBody ModifyUserPasswordVo vo) {
        userAuthService.updatePassword(vo);
        return Result.ok();
    }

    @Log(SELECT)
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "current", value = "当前页", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "size", value = "每页显示数量", dataTypeClass = Long.class)
            }
    )
    @ApiOperation(value = "在线用户列表")
    @GetMapping("/admin/user/online/listPage")
    public Result<List<UserOnlineDTO>> onlineListPage(@RequestParam(value = "current", defaultValue = "1") Long current,
                                                      @RequestParam(value = "size", defaultValue = "10") Long size,
                                                      UserOnlineListPageSearchVo condition) {
        List<UserOnlineDTO> result = userInfoService.onlineListPage(current, size, condition);
        return Result.ok(result);
    }

    @Log(DELETE)
    @ApiImplicitParam(name = "id", value = "用户信息id", required = true, dataTypeClass = Long.class)
    @ApiOperation(value = "强制用户下线")
    @DeleteMapping("/admin/user/offline")
    public Result<String> offline(@NotNull(message = "用户信息id不能为空") @RequestBody Long userInfoId) {
        userInfoService.offline(userInfoId);
        return Result.ok();
    }

    @DeleteMapping("/user/{id}")
    public Result<String> deleteById(@PathVariable("id") Long id) {
        userAuthService.removeById(id);
        return Result.ok();
    }
}

